﻿using Microsoft.Office.Interop.Excel;

//using Excel;
using System;
using System.IO;
using System.Windows.Forms;

//using Excel;

namespace test.word.excel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ApplicationClass ExcelApp = new ApplicationClass();
            ExcelApp.Caption = "应用程序调用 Microsoft Excel";
            ExcelApp.Visible = true;
            //string workbookPath = Environment.CurrentDirectory + "\\2.xls";
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

            Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
            string currentSheet = "Sheet1";
            Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);

            Microsoft.Office.Interop.Excel.Range excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range("A1");
            excelCell.Value2 = "Hello@233311\nhi";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ApplicationClass ExcelApp = new ApplicationClass();
            ExcelApp.Caption = "应用程序调用 Microsoft Excel";
            //ExcelApp.Visible = true;
            ExcelApp.Visible = false;
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
            string currentSheet = "Sheet1";
            Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);
            Microsoft.Office.Interop.Excel.Range excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range("a1");
            excelCell.Value2 = "Hello world";
            //ExcelApp.SaveWorkspace("Demo1.xls");
            excelWorkbook.SaveAs(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Demo1.xlsx"));
        }
    }
}